*First stage statistics (Table B.3)

*Bring in regression data
use regression_data.dta, clear

****************************************************************************

egen clusterId=group(originId)

*drop ROW as an originId
drop if originId==62

*Collapsed version: average across importers
collapse logX_theta logL* instrument_* logX_adj logZ_adj clusterId, by(year sectorId originId)

**************************************************************************************
*Multiple Elasticities: Heterogeneous TE
**************************************************************************************

*FE
local dvars logX_theta
rename logL nib
rename instrument_base ihpn
local indvars c.nib#sectorId
local instruments c.ihpn#sectorId


*OLS
*eststo r_D: reghdfe `dvars' `indvars' if sectorId>2, cluster(clusterId) ///
*a(destId#sectorId#year pairId#year)
*outreg2 using ${tab_dir}/Jan_2019_draft/table3.xml, replace excel noaster dec(2)   nor2 addstat(Within R-squared, e(r2_within)) nopa

*IV with CD instrument
*eststo r_D: reghdfe `dvars' (`indvars'=`instruments') if sectorId>2, cluster(clusterId) ///
*a(destId#sectorId#year pairId#year) ivsuite(ivreg2)  subopt(savefirst)

*CES Instrument
*eststo r_D: reghdfe `dvars'  (`indvars'=`instruments') if sectorId>2, cluster(clusterId) ///
*a(destId#sectorId#year pairId#year) ivsuite(ivreg2) stage(first)  subopt(savefirst)
*outreg2  using ${tab_dir}/Jan_2019_draft/table3.xml, append excel nor2 dec(2)  addstat(F-stat, e(widstat),Within R-squared, e(r2_within)) noaster nopa

gen dum=1 if sectorId==3
replace dum=0 if dum==.
gen depfirst=dum*nib
eststo r_D: reghdfe depfirst `instruments' if sectorId>2 & year==2010, cluster(clusterId) ///
a(sectorId#year originId#year)
outreg2  using table3_fs.xml, replace excel nor2 dec(2)  addstat(Within R-squared, e(r2_within)) noaster nopa
drop depfirst dum


forvalues x=4/17	{
gen dum=1 if sectorId==`x'
replace dum=0 if dum==.
gen depfirst=dum*nib
eststo r_D: reghdfe depfirst `instruments' if sectorId>2 & year==2010, cluster(clusterId) ///
a(sectorId#year originId#year)
outreg2  using table3_fs.xml, append excel nor2 dec(2)  addstat(Within R-squared, e(r2_within)) noaster nopa
drop dum depfirst
}
	


